package cn.itsource.org.service.impl;

import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service//加载当前service类
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//添加事务，只读，没有事务就以非事务执行
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> findAll() {
        return employeeMapper.loadAll();
    }

    @Override
    public Employee findById(Long id) {
        return employeeMapper.loadById(id);
    }

    @Transactional//增删改需要事务
    @Override
    public void add(Employee employee) {
        employeeMapper.save(employee);
    }

    @Transactional//增删改需要事务
    @Override
    public void delById(Long id) {
        employeeMapper.remove(id);
    }

    @Transactional//增删改需要事务
    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public PageList<Employee> queryPage(EmployeeQuery employeeQuery) {
        //查询总条数
        Integer count = employeeMapper.queryCount(employeeQuery);
        //总条数为0或者null，不需要分页查询了，直接返回结果
        if (count==null || count==0){
            return  new PageList<Employee>();
        }
        //查询当前页数据
        List<Employee> list = employeeMapper.queryData(employeeQuery);
        //调用全参构造
        return new PageList<Employee>(count,list);
    }

    @Override
    public void parchDel(List<Long> ids) {
        employeeMapper.parchDel(ids);
    }
}
